package com.xian.tszs.meal.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.xian.tszs.meal.annotation.UserLoginToken;
import com.xian.tszs.meal.dto.OrderGenerate;
import com.xian.tszs.meal.entity.TsUser;
import com.xian.tszs.meal.repository.TsUserRepository;
import com.xian.tszs.meal.server.WebSocketServer;
import com.xian.tszs.meal.service.FrontOrderService;
import com.xian.tszs.meal.utils.TokenUtil;

/***
 * 前台订单相关
 * 
 * @Title: FrontOrderController.java
 * @author MRC
 * @date 2019年5月22日 下午4:22:29
 * @version V1.0
 */
@Controller
@RequestMapping("wechat/order")
public class FrontOrderController {

	@Autowired
	private TsUserRepository tsUserRep;

	@Autowired
	private FrontOrderService frontOrderService;

	/***
	 * 
	 * @author: MRC
	 * @date 2019年5月22日 下午4:28:52
	 * @return Map<String,Object> 返回类型
	 */
	@UserLoginToken
	@PostMapping("generateOrder")
	@ResponseBody
	public Map<String, Object> generateOrderBy(@RequestBody OrderGenerate orderGenerate) {

		Map<String, Object> map = new HashMap<>();
		String uId = TokenUtil.getTokenUserId();
		TsUser u = tsUserRep.findById(uId).get();

		map.put("success", false);
		if (u == null) {
			map.put("msg", "您还未登录！");
			return map;
		} else if (StringUtils.isEmpty(orderGenerate.getBusinessId())) {
			map.put("msg", "参数缺失！");
			return map;
		}
		String orderNumber = frontOrderService.generateOrder(u,orderGenerate);
		map.put("success", true);
		map.put("order", orderNumber);
		
		try {
			WebSocketServer.sendInfo("您有新的订单了！请注意查收", orderGenerate.getBusinessId());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return map;
	}
	
	
	/***
	 * 用户订单信息
	* @author: MRC  
	* @date 2019年5月24日 上午9:12:36 
	* @return Map<String,Object> 返回类型
	 */
	@UserLoginToken
	@PostMapping("findUserOrder")
	@ResponseBody
	public Map<String, Object> findUserOrder(String status) {
		
		String uId =TokenUtil.getTokenUserId();
		TsUser u = tsUserRep.findById(uId).get();
		Map<String, Object> map = new HashMap<>();
		
		map.put("success", false);
		if (StringUtils.isEmpty(status)) {
			map.put("msg", "参数缺失！");
		} else if (u == null) {
			map.put("msg", "用户信息不存在！");
		} else {
			map.put("order",frontOrderService.findUserOrder(status,uId));
		}
		return map;
	}
	
	/***
	 * 取消订单
	* @author: MRC  
	* @date 2019年5月24日 下午2:20:59 
	* @return Map<String,Object> 返回类型
	 */
	@UserLoginToken
	@PostMapping("cancelOrder")
	@ResponseBody
	public Map<String, Object> cancelOrder(String id) {
		
		String uId =TokenUtil.getTokenUserId();
		TsUser u = tsUserRep.findById(uId).get();
		Map<String, Object> map = new HashMap<>();
		
		map.put("success", false);
		if (StringUtils.isEmpty(id)) {
			map.put("msg", "参数缺失！");
		} else if (u == null) {
			map.put("msg", "用户信息不存在！");
		} else {
			map.put("success", true);
			map.put("msg", "订单取消成功！");
			frontOrderService.cancelOrder(id);
		}
		
		return map;
	}
	
}
